package org.adream.account.service;

import java.io.IOException;
import java.util.Date;

import org.adream.account.dao.UserDao;
import org.adream.account.dao.UserOpenIdDao;
import org.adream.account.entity.UserEntity;
import org.adream.account.entity.UserOpenIdEntity;
import org.adream.account.util.Constant;
import org.adream.account.util.SensorDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sensorsdata.analytics.javasdk.SensorsAnalytics;
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;

/**
 * 第三方绑定 的表操作service
 * 
 * @author chenhaoyu
 *
 */
@Service
public class OAuthService {
	
	private static final Logger logger = LoggerFactory.getLogger(OAuthService.class);

	@Autowired
	private UserDao userDao;

	@Autowired
	private UserOpenIdDao openIdDao;
	
	@Autowired
	private SensorDataUtil sensorDataUtil;
	
	public enum OAUTHTYPE {
		WECHAT,ALIPAY,QQ,SINA
	};

	/**
	 * 
	 * @param type
	 *            需要绑定的第三方
	 * @param uid
	 *            用户uid
	 * @param nickName
	 * 			  第三方nickName
	 * @param unionid
	 *            第三方唯一凭证
	 * @param extra
	 *            额外参数 比如微信需要openid和appid
	 * @throws ADreamServiceException
	 * @throws IOException 
	 * @throws InvalidArgumentException 
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = ADreamServiceException.class, isolation = Isolation.DEFAULT)
	public void bind(OAUTHTYPE type, String uid, String nickName, String unionid, String extra) throws ADreamServiceException, IOException, InvalidArgumentException {
		logger.info("绑定第三方账号");
		
		if(StringUtils.isEmpty(type) || StringUtils.isEmpty(uid) || StringUtils.isEmpty(unionid)) {
			logger.info("绑定失败,请求参数不能为空:)");
			throw new ADreamServiceException("请求参数不能为空");
		}
		
		UserEntity user = userDao.queryUserByUid(uid);
		if(user == null) {
			logger.info("绑定失败,未查询到相应的user(:");
			throw new ADreamServiceException("未查询到相应的数据");
		}
		
		switch (type) {
		case WECHAT:
			user.setUnionid(unionid);
			user.setNickName(nickName);
			//微信openid
			if(!StringUtils.isEmpty(extra) && extra.split("_").length == Constant.TWO) {				
				UserOpenIdEntity openId = new UserOpenIdEntity();
				openId.setOpenId(extra.split("_")[0]);
				openId.setAppId(extra.split("_")[1]);
				openId.setUid(uid);
				openIdDao.addOpenId(openId);
			}
			//神策数据设置用户属性 'unionid'
			SensorsAnalytics sa = sensorDataUtil.getSensorsAnalytics();
			sa.profileSet(uid, true, "union_id", unionid);
			break;

		case ALIPAY:
			user.setApUnionid(unionid);
			break;

		case QQ:
			user.setQqUnionid(unionid);
			break;

		case SINA:
			user.setMbUnionid(unionid);
			break;

		default:
			throw new ADreamServiceException("类型错误");
		}
		user.setModifier(uid);
		user.setMts(new Date());
		userDao.updateUser(user);
	}

	/**
	 * 解绑
	 * 
	 * @param uid
	 * @throws ADreamServiceException 
	 * @throws IOException 
	 * @throws InvalidArgumentException 
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = ADreamServiceException.class, isolation = Isolation.DEFAULT)
	public void unbind(OAUTHTYPE type,String uid) throws ADreamServiceException, IOException, InvalidArgumentException {
		logger.info("解绑相关第三方");
		if(StringUtils.isEmpty(type) || StringUtils.isEmpty(uid)) {
			logger.info("解绑失败,请求参数有误:)");
			throw new ADreamServiceException("解绑失败,请求参数有误");
		}
		UserEntity user = userDao.queryUserByUid(uid);
		if(user == null) {
			logger.info("解绑失败,无相关user:)");
			throw new ADreamServiceException("解绑失败,无相关user");
		}
		switch (type) {
		case WECHAT:
			user.setNickName(null);
			user.setUnionid(null);
			openIdDao.deleteOpenIdByUid(uid);
			
			//神策数据设置用户属性 'unionid'
			SensorsAnalytics sa = sensorDataUtil.getSensorsAnalytics();
			sa.profileSet(uid, true, "union_id", "");
			break;
		case QQ:
			user.setQqUnionid(null);
			break;
		case ALIPAY:
			user.setApUnionid(null);
			break;
		case SINA:
			user.setMbUnionid(null);
			break;
		}
		user.setModifier(uid);
		user.setMts(new Date());
		userDao.updateUser(user);
	}
}